﻿@model IEnumerable<CoreLims.Models.CoreLims.SampleAnalyte>

@{
    Layout = null;
}

<div>
    <div class="input-group">
        <div class="col-3 pr-0" style="display: none">
            <label id="SAMPLE-AddAnalyte-AnalyteId">@ViewData["AnalyteId"]</label>
            <label id="SAMPLE-AddAnalyte-Parent-ModalID">@ViewData["ModalID"]</label>
        </div>
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">代码*</span>
        </div>
        <div class="col-9 pl-0">
            <input id="SAMPLE-AddAnalyte-Code" class="form-control font-rfs border-radius-noleft" placeholder="必填" />
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">英文名</span>
        </div>
        <div class="col-9 pl-0">
            <input id="SAMPLE-AddAnalyte-English" class="form-control font-rfs border-radius-noleft" placeholder="可选" />
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">中文名*</span>
        </div>
        <div class="col-9 pl-0">
            <input id="SAMPLE-AddAnalyte-Name" class="form-control font-rfs border-radius-noleft" placeholder="必填" />
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">分类*</span>
        </div>
        <div class="col-9 pl-0 input-group position-relative">
            <select id="SAMPLE-AddAnalyte-Select-Classification" class="form-control font-rfs border-radius-noleft" style="width:90%">
                @foreach (var item in Model)
                {
                    <option>@item.Classification</option>
                }
            </select>
            <div class="col-11 pl-0 input-group position-absolute float-left"><input id="SAMPLE-AddAnalyte-Input-Classification" class="form-control font-rfs border-radius-noleft noRightBorder" style="width:100%" placeholder="可输入新的类型" /></div>
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">描述</span>
        </div>
        <div class="col-9 pl-0">
            <input id="SAMPLE-AddAnalyte-Description" class="form-control font-rfs border-radius-noleft" placeholder="可选" />
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">结果模式*</span>
        </div>
        <div class="col-9 pl-0">
            <select id="SAMPLE-AddAnalyte-Select-ResultModel" class="form-control font-rfs border-radius-noleft">
                <option>string</option>
                <option>int</option>
                <option>decimal</option>
            </select>
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">修约</span>
        </div>
        <div class="col-9 pl-0 input-group">
            <select id="SAMPLE-AddAnalyte-RoundingOff1" class="form-control font-rfs border-radius-noleft" style="width:40%">
                <option>ISO</option>
                <option>FDA</option>
                <option>ENOB</option>
            </select>
            <span class="input-group-text font-rfs border-radius-noradius noRightBorder noLeftBorder" style="width:30%">小数位数：</span>
            <input id="SAMPLE-AddAnalyte-RoundingOff2" class="form-control font-rfs border-radius-noleft" style="width:30%" placeholder="1位整数" maxlength="1" onkeyup="value=value.replace(/[^\d]/g,'')"/>
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">原始结果单位</span>
        </div>
        <div class="col-9 pl-0 input-group">
            <select id="SAMPLE-AddAnalyte-UnitsType" class="form-control font-rfs border-radius-noleft" style="width:45%"></select>
            <select id="SAMPLE-AddAnalyte-Units" class="form-control font-rfs border-radius-noleft" style="width:45%"></select>
            <button id="SAMPLE-AddAnalyte-AddUnits" class="input-group-text font-rfs border-radius-noleft noLeftBorder pl-2" style="width:10%" title="添加单位"><i class="bi-plus"></i></button>
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">最终结果单位</span>
        </div>
        <div class="col-9 pl-0 input-group">
            <select id="SAMPLE-AddAnalyte-FUnitsType" class="form-control font-rfs border-radius-noleft" style="width:45%"></select>
            <select id="SAMPLE-AddAnalyte-FUnits" class="form-control font-rfs border-radius-noleft" style="width:45%"></select>
            <button id="SAMPLE-AddAnalyte-AddUnits" class="input-group-text font-rfs border-radius-noleft noLeftBorder pl-2" style="width:10%" title="添加单位"><i class="bi-plus"></i></button>
        </div>
    </div>
    <div id="SAMPLE-AddAnalyte-Spec-div" class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">默认指标</span>
        </div>
        <div class="col-9 pl-0 input-group">
            <input id="SAMPLE-AddAnalyte-Spec" class="form-control font-rfs border-radius-noleft" style="width:90%" data-id="" disabled="disabled" />
            <button id="SAMPLE-AddAnalyte-Select-Spec" class="input-group-text font-rfs border-radius-noleft noLeftBorder pl-2" style="width:10%" title="添加或更改默认指标"><i class="bi-plus"></i></button>
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">检出限</span>
        </div>
        <div class="col-9 pl-0">
            <input id="SAMPLE-AddAnalyte-DetectionLimit" class="form-control font-rfs border-radius-noleft" placeholder="可选" />
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">默认值</span>
        </div>
        <div class="col-9 pl-0">
            <input id="SAMPLE-AddAnalyte-DefaultValue" class="form-control font-rfs border-radius-noleft" placeholder="可选" />
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">备选结果</span>
        </div>
        <div class="col-9 pl-0">
            <input id="SAMPLE-AddAnalyte-AlternativeResults" class="form-control font-rfs border-radius-noleft" placeholder="可选" />
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">关联方法</span>
        </div>
        <div class="col-9 pl-0 input-group">
            <select id="SAMPLE-AddAnalyte-MethodType" class="form-control font-rfs border-radius-noleft"></select>
            <select id="SAMPLE-AddAnalyte-MethodCode" class="form-control font-rfs border-radius-noleft"></select>
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">报告过滤</span>
        </div>
        <div class="col-9 pl-0">
            <select id="SAMPLE-AddAnalyte-Assist" class="form-control font-rfs border-radius-noleft">
                <option>否</option>
                <option>是</option>
            </select>
        </div>
    </div>
    <div class="text-right">
        <button id="Supplier-AddSupplier-confirm" class="btn btn-primary font-rfs">确定</button>
        <button id="Supplier-AddSupplier-cancel" type="button" class="btn btn-info font-rfs">取消</button>
    </div>
</div>
<div class="modal" id="SAMPLE-AddAnalyte-AddUnits-Modal" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class='modal-header'>
                <p id="SAMPLE-AddAnalyte-AddUnits-modalTitle" class="pt-3 pl-3"></p>
                <a data-dismiss="modal" class="modal-close">&times;</a>
                <label id="ModalID" style="display:none">SAMPLE-AddAnalyte-AddUnits-Modal</label>
            </div>
            <div class="modal-body">
                <div id="SAMPLE-AddAnalyte-AddUnits-Embed"></div>
            </div>
        </div>
    </div>
</div>

<script>
    var AddAnalyte_analyteId = $("#SAMPLE-AddAnalyte-AnalyteId").html();

    $(document).ready(function () {
        $("#SAMPLE-AddAnalyte-Select-Classification").prepend(new Option("", ""));
        $("#SAMPLE-AddAnalyte-Select-Classification").val("");

        LoadUnitType();
        LoadMethodType();
    });

    /*加载单位类型*/
    function LoadUnitType() {
        $.post("GetUnitType", {
            //    Id: analyteId
        }, function (data) {
            for (var i = 0; i < data.length; i++) {
                $("#SAMPLE-AddAnalyte-UnitsType").append(new Option(data[i].type));
                $("#SAMPLE-AddAnalyte-FUnitsType").append(new Option(data[i].type));
            }
            $("#SAMPLE-AddAnalyte-UnitsType").prepend(new Option("", ""));
            $("#SAMPLE-AddAnalyte-UnitsType").val("");
            $("#SAMPLE-AddAnalyte-FUnitsType").prepend(new Option("", ""));
            $("#SAMPLE-AddAnalyte-FUnitsType").val("");

            /*初始化页面控件值*/
            if (AddAnalyte_analyteId != '' && AddAnalyte_analyteId != null && AddAnalyte_analyteId != undefined) {
                $.post("GetAnalyte", {
                    Id: AddAnalyte_analyteId
                }, function (data) {
                    $("#SAMPLE-AddAnalyte-Code").val(data.code);  /*这个地方属性首字母都是小写，否则数据绑定不上*/
                    $("#SAMPLE-AddAnalyte-English").val(data.english);
                    $("#SAMPLE-AddAnalyte-Name").val(data.name);
                    $("#SAMPLE-AddAnalyte-Input-Classification").val(data.classification);
                    $("#SAMPLE-AddAnalyte-Description").val(data.description);
                    $("#SAMPLE-AddAnalyte-Select-ResultModel").val(data.resultModel);
                    var st = data.roundingOff.split(",")
                    $("#SAMPLE-AddAnalyte-RoundingOff1").val(st[0]);
                    $("#SAMPLE-AddAnalyte-RoundingOff2").val(st[1]);
                    $("#SAMPLE-AddAnalyte-DetectionLimit").val(data.detectionLimit);
                    $("#SAMPLE-AddAnalyte-DefaultValue").val(data.defaultValue);
                    $("#SAMPLE-AddAnalyte-AlternativeResults").val(data.alternativeResults);
                    $("#SAMPLE-AddAnalyte-MethodCode").val(data.methodCode);
                    $("#SAMPLE-AddAnalyte-MethodCode").prepend(new Option(data.methodCode, data.methodId));

                    if (data.unitsType.length != 0) {
                        $("#SAMPLE-AddAnalyte-UnitsType").val(data.unitsType);
                        $.post("/Common/GetUnitByTypeall", {  /*根据类型，加载单位*/
                            Type: $("#SAMPLE-AddAnalyte-UnitsType").find("option:selected").text()
                        }, function (data) {
                            var count = $("#SAMPLE-AddAnalyte-Units option").length;
                            if (count > 0) {
                                count = count - 1;
                            }
                            for (var i = 0; i < count; i++) {
                                $("#SAMPLE-AddAnalyte-Units  option").remove(0);
                            }
                            for (var i = 0; i < data.length; i++) {
                                $("#SAMPLE-AddAnalyte-Units").append(new Option(data[i].code));
                            }
                        });
                        $("#SAMPLE-AddAnalyte-Units").val(data.units);
                    }
                    /*绑定默认指标*/
                    if (data.defaultSpecId != '' && data.defaultSpecId != null && data.defaultSpecId != undefined) {
                        $("#SAMPLE-AddAnalyte-Spec").val(data.defaultSpecName);
                        $("#SAMPLE-AddAnalyte-Spec").attr("data-id", data.defaultSpecId);
                    }
                });
            } else {   /*如果是添加新项，结果模式添加空值*/
                $("#SAMPLE-AddAnalyte-RoundingOff1").prepend(new Option("", ""));
                $("#SAMPLE-AddAnalyte-RoundingOff1").val("");
                /*添加新项时，数据库里还没有数据无法更新，所以不显示。添加成功后再次编辑才能设定默认指标。*/
                $("#SAMPLE-AddAnalyte-Spec-div").hide();
            }
        });
    }

    // 加载方法列表
    function LoadMethodType() {
        $.post("GetMethodType", {
        }, function (data) {
            for (var i = 0; i < data.length; i++) {
                $("#SAMPLE-AddAnalyte-MethodType").append(new Option(data[i].type));
            }
            $("#SAMPLE-AddAnalyte-MethodType").prepend(new Option("", ""));
            $("#SAMPLE-AddAnalyte-MethodType").val("");
        });
    }

    $("#SAMPLE-AddAnalyte-MethodType").change(function () {
        $.post("GetMethodCodeByType", {
            Type: $("#SAMPLE-AddAnalyte-MethodType").find("option:selected").text()
        }, function (data) {
            $("#SAMPLE-AddAnalyte-MethodCode").empty();
            for (var i = 0; i < data.length; i++) {
                $("#SAMPLE-AddAnalyte-MethodCode").append(new Option(data[i].code, data[i].id));
            }
            $("#SAMPLE-AddAnalyte-MethodCode").prepend(new Option("", ""));
            $("#SAMPLE-AddAnalyte-MethodCode").val("");
        });
    });

    $("#SAMPLE-AddAnalyte-UnitsType").change(function () {
        $.post("/Common/GetUnitByTypeall", {  /*根据类型，加载单位*/
            Type: $("#SAMPLE-AddAnalyte-UnitsType").find("option:selected").text()
        }, function (data) {
            var count = $("#SAMPLE-AddAnalyte-Units option").length;
            if (count > 0) {
                count = count - 1;
            }
            for (var i = 0; i < count; i++) {
                $("#SAMPLE-AddAnalyte-Units  option").remove(0);
            }
            for (var i = 0; i < data.length; i++) {
                $("#SAMPLE-AddAnalyte-Units").append(new Option(data[i].code));
            }
        });
    });

    $("#SAMPLE-AddAnalyte-FUnitsType").change(function () {
        $.post("/Common/GetUnitByTypeall", {  /*根据类型，加载单位*/
            Type: $("#SAMPLE-AddAnalyte-FUnitsType").find("option:selected").text()
        }, function (data) {
            var count = $("#SAMPLE-AddAnalyte-FUnits option").length;
            if (count > 0) {
                count = count - 1;
            }
            for (var i = 0; i < count; i++) {
                $("#SAMPLE-AddAnalyte-FUnits  option").remove(0);
            }
            for (var i = 0; i < data.length; i++) {
                $("#SAMPLE-AddAnalyte-FUnits").append(new Option(data[i].code));
            }
        });
    });

    $("#SAMPLE-AddAnalyte-Select-Classification").change(function () {
        $("#SAMPLE-AddAnalyte-Input-Classification").val($("#SAMPLE-AddAnalyte-Select-Classification").find("option:selected").text());
    });

    $("#Supplier-AddSupplier-confirm").click(function () {
        if ($.trim($("#SAMPLE-AddAnalyte-Code").val()).length == 0) {
            alert("分析项代码不能为空！");
            $("#SAMPLE-AddAnalyte-Code").addClass('error-style');
            return false;
        }
        if ($.trim($("#SAMPLE-AddAnalyte-Name").val()).length == 0) {
            alert("分析项名称不能为空！");
            $("#SAMPLE-AddAnalyte-Name").addClass('error-style');
            return false;
        }
        if ($.trim($("#SAMPLE-AddAnalyte-Input-Classification").val()).length == 0) {
            alert("分析项类型不能为空！");
            $("#SAMPLE-AddAnalyte-Input-Classification").addClass('error-style');
            return false;
        }
        var roundingOff = "";
        if ($.trim($("#SAMPLE-AddAnalyte-RoundingOff1").find("option:selected").text()).length != 0) {
            if ($("#SAMPLE-AddAnalyte-RoundingOff2").val().length == 0) {
                alert("修约有效小数位数不能为空！");
                $("#SAMPLE-AddAnalyte-RoundingOff2").addClass('error-style');
                return false;
            } else {
                roundingOff = $("#SAMPLE-AddAnalyte-RoundingOff1").find("option:selected").text() + "," + $("#SAMPLE-AddAnalyte-RoundingOff2").val();
            }
        }

        var jsonStr = {};
        jsonStr["Code"] = $.trim($("#SAMPLE-AddAnalyte-Code").val());
        jsonStr["English"] = $.trim($("#SAMPLE-AddAnalyte-English").val());
        jsonStr["Name"] = $.trim($("#SAMPLE-AddAnalyte-Name").val());
        jsonStr["Classification"] = $.trim($("#SAMPLE-AddAnalyte-Input-Classification").val());
        jsonStr["Description"] = $.trim($("#SAMPLE-AddAnalyte-Description").val());
        jsonStr["ResultModel"] = $.trim($("#SAMPLE-AddAnalyte-Select-ResultModel").find("option:selected").text());
        jsonStr["RoundingOff"] = roundingOff;
        jsonStr["Units"] = $("#SAMPLE-AddAnalyte-Units").val();
        jsonStr["FUnits"] = $("#SAMPLE-AddAnalyte-FUnits").val();
        jsonStr["UnitsType"] = $("#SAMPLE-AddAnalyte-UnitsType").find("option:selected").text();
        jsonStr["DetectionLimit"] = $.trim($("#SAMPLE-AddAnalyte-DetectionLimit").val());
        jsonStr["DefaultValue"] = $.trim($("#SAMPLE-AddAnalyte-DefaultValue").val());
        jsonStr["AlternativeResults"] = $.trim($("#SAMPLE-AddAnalyte-AlternativeResults").val());
        jsonStr["MethodId"] = $("#SAMPLE-AddAnalyte-MethodCode").val();
        jsonStr["MethodCode"] = $("#SAMPLE-AddAnalyte-MethodCode").find("option:selected").text();

        if ($.trim($("#SAMPLE-AddAnalyte-Spec").val()).length != 0) {
            jsonStr["DefaultSpecName"] = $("#SAMPLE-AddAnalyte-Spec").val();
            jsonStr["DefaultSpecId"] = $("#SAMPLE-AddAnalyte-Spec").attr('data-id');
        }
        if ($("#SAMPLE-AddAnalyte-Assist").find("option:selected").text() == "是") {
            jsonStr["Assist"] = true;   
        } else {
            jsonStr["Assist"] = false;
        }

        if (AddAnalyte_analyteId == "") {
            $.post("postAddAnalyte", {
                jsonStr: JSON.stringify(jsonStr)
            }, function (data) {
                alert(data);
                $("#SAMPLE-AddAnalyte-file").attr("data-id", ""); // 提交成功后置空，不影响后续操作
                var modalID = $("#SAMPLE-AddAnalyte-Parent-ModalID").html();
                //$("#" + modalID).modal('hide');
            });
        } else {
            jsonStr["Id"] = $("#SAMPLE-AddAnalyte-AnalyteId").html();
            $.post("postUpdateAnalyte", {
                jsonStr: JSON.stringify(jsonStr)
            }, function (data) {
                if (data.indexOf("成功") >= 0) {
                    $("#SAMPLE-AddAnalyte-file").attr("data-id", ""); // 提交成功后置空，不影响后续操作
                    var modalID = $("#SAMPLE-AddAnalyte-Parent-ModalID").html();
                    $("#" + modalID).modal('hide');
                } else {
                    alert(data);
                }
            });
        }
    });

    $("#Supplier-AddSupplier-cancel").click(function () {
        var modalID = $("#SAMPLE-AddAnalyte-Parent-ModalID").html();
        $("#" + modalID).modal('hide');
    });

    $("#SAMPLE-AddAnalyte-AddUnits").click(function () {
        $("#SAMPLE-AddAnalyte-AddUnits-modalTitle").text("添加单位");

        $("#SAMPLE-AddAnalyte-AddUnits-Modal").modal({
            backdrop: 'static',
            keyboard: false
        });

        $("#SAMPLE-AddAnalyte-AddUnits-Embed", parent.document.body).attr("src", "AddUnit");
        $.post("AddUnit", function (datas) {
            $("#SAMPLE-AddAnalyte-AddUnits-Embed").html(datas);
        });
    });
    
    $("#SAMPLE-AddAnalyte-Select-Spec").click(function() {
        $("#SAMPLE-AddAnalyte-AddUnits-modalTitle").text("选择默认指标");

        $("#SAMPLE-AddAnalyte-AddUnits-Modal").modal({
            backdrop: 'static',
            keyboard: false
        });

        $("#SAMPLE-AddAnalyte-AddUnits-Embed", parent.document.body).attr("src", "SelectSpec");
        $.post("SelectSpec", { 
            Id: $("#SAMPLE-AddAnalyte-AnalyteId").html()
        }, function (datas) {
            $("#SAMPLE-AddAnalyte-AddUnits-Embed").html(datas);
        });
    });

    $('#SAMPLE-AddAnalyte-AddUnits-Modal').on('hidden.bs.modal', function (event) {
        if ($('#SAMPLE-AddAnalyte-AnalyteId').html().length != 0) {
            $.post("GetAnalyte", {
                Id: $('#SAMPLE-AddAnalyte-AnalyteId').html()
            }, function (data) {
                $('#SAMPLE-AddAnalyte-Spec').val(data.defaultSpecName);
                $('#SAMPLE-AddAnalyte-Spec').attr("data-id", data.defaultSpecId);
            });
        }
    });

</script>
